package com.example.dzcfinal;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dzcfinal.SQLite.SQLite;

public class Index2 extends Activity {

    private LinearLayout goumai;
    private TextView name;
    private TextView type;
    private TextView writer;
    private String nameText;
    private String typeText;
    private String writerText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.index2);
        initHandle();
        clickGoumai();
    }
    //    将控件获取赋值
    protected void initHandle(){
        goumai = findViewById(R.id.goumai);
        name = findViewById(R.id.name);
        type =findViewById(R.id.type);
        writer = findViewById(R.id.writer);

        nameText = name.getText().toString();
        typeText = type.getText().toString();
        writerText = writer.getText().toString();
    }

    //    添加按钮点击事件
    private void clickGoumai(){
        goumai.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SQLite sql = new SQLite(Index2.this);
                if (sql.findGood(nameText)!=-1){
                    Toast.makeText(Index2.this, "已经购买", Toast.LENGTH_SHORT).show();
                }else {
                    sql.insertGood(nameText,typeText,writerText);
                    Toast.makeText(Index2.this, "购买成功", Toast.LENGTH_SHORT).show();
                }
            }

        });
    }
}
